package com.croa.task;

import com.dix.base.common.Const;
import com.dix.base.core.UserContext;
import com.dix.base.interceptor.SecurityInterceptor;
import com.dix.base.model.security.Token;
import com.dix.base.security.TokenService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

public class UserAuthenticator implements SecurityInterceptor.Authenticator {

    @Autowired
    private TokenService tokenService;

    @Override
    public void process(HttpServletRequest httpServletRequest) {
        String token = SecurityInterceptor.getRequiredParam(httpServletRequest, "token");
        Token tokenModel = tokenService.getToken(token);
        UserContext userContext = new UserContext();
        userContext.setUserId(tokenModel.getUserId());
        userContext.setUserType(tokenModel.getUserType());
        httpServletRequest.setAttribute(Const.REQUEST_ATTR_KEY_USER_CONTEXT, userContext);
        httpServletRequest.setAttribute("token", token);
    }
}
